import functools
from flask import current_app
from flask import g
from flask import session


def login_user_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 1.获取user_id
        user_id = session.get("user_id")

        # 2.根据user_id查询用户对象
        user = None  # type: User
        try:
            if user_id:
                from info.models import User
                user = User.query.get(user_id)
        except Exception as error:
            current_app.logger.error(error)

        # 3.保存用户对象给被装饰的视图函数使用
        g.user = user
        # 在视图函数中使用g对象获取user对象进而使用
        # 只要在本次请求范围内（request没有结束）就能获取g对象的内容
        return view_func(*args, **kwargs)
    return wrapper



def do_index_class(index):
    """根据传入的index值返回对应的class属性"""
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""